package me.opbukkitdemo;

import me.opbukkitdemo.common.event.PlayerOpChangeEvent;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

public final class Bukkit extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onPlayerOpChange(PlayerOpChangeEvent event) {
        Player player = event.getPlayer();
        if (event.isNewOp()) {
            player.sendMessage("You are now an operator.");
        } else {
            player.sendMessage("You are no longer an operator.");
        }
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (!(sender instanceof Player player)) return false;
        boolean op = player.isOp();
        player.setOp(!op);
        return true;
    }

}
